package rank1300;

/**
 * 2256. 最小平均差
 */
public class No2256 {
    public int minimumAverageDifference(int[] nums) {
        int n = nums.length;
        long minDiff = Long.MAX_VALUE;
        long preSum = 0, sufSum = 0;
        for (int num : nums) {
            sufSum += num;
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            preSum += nums[i];
            sufSum -= nums[i];
            long abs = Math.abs(preSum / (i + 1) - sufSum / (n - i - 1 == 0 ? 1 : (n - i - 1)));
            if (abs < minDiff) {
                minDiff = abs;
                ans = i;
            }
        }
        return ans;
    }
}
